package com.lei.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lei.eduservice.entity.EduCourse;
import com.lei.eduservice.entity.vo.CourseInfoVo;
import com.lei.eduservice.entity.vo.CoursePublishVo;
import com.lei.eduservice.entity.vo.CourseQueryVo;
import com.lei.eduservice.service.EduCourseService;
import com.lei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/edu/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;


    // 分页+查询 课程列表
    @PostMapping("/list")
    public Result courseList(@RequestBody(required = false) CourseQueryVo courseQueryVo){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQueryVo.getTitle();
        Integer current = courseQueryVo.getCurrent();
        Integer limit = courseQueryVo.getLimit();
        String status = courseQueryVo.getStatus();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if (current==null){
            current=1;
        }
        if (limit==null){
            limit=10;
        }
        wrapper.orderByAsc("gmt_create");
        Page<EduCourse> coursePage = new Page<>(current,limit);
        courseService.page(coursePage,wrapper);


        return Result.ok().data("records",coursePage.getRecords()).data("total",coursePage.getTotal());
    }

    //添加课程基本信息
    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfo){
        String s = courseService.saveCourseInfo(courseInfo);
        return Result.ok().data("courseId",s);
    }
    //根据课程Id查课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public Result getCourseInfoById(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //根据课程Id查课程发布信息
    @GetMapping("/getPublishCourse/{courseId}")
    public Result getPublishCourseInfo(@PathVariable("courseId") String courseId){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(courseId);

        return Result.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    @PostMapping("/publishCourse/{courseId}")
    public Result publishCourse(@PathVariable("courseId") String courseId){
        courseService.updateById(courseService.getById(courseId).setStatus("Normal"));
        return Result.ok();
    }

    @DeleteMapping("/{courseId}")
    public Result deleteCourse(@PathVariable("courseId") String courseId){
        courseService.deleteCourse(courseId);
        return Result.ok();
    }

}

